package com.luck.easyfoodbatch.mvp.order;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.MyOrdersM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.order
 * email：893855882@qq.com
 * data：16/11/9
 */
public class MyOrdersPresenter implements MyOrdersModel {

    private final String TAG = getClass().getSimpleName();
    private MyOrdersView myOrdersView;
    private Context mContext;

    public MyOrdersPresenter(MyOrdersView myOrdersView, Context mContext) {
        this.myOrdersView = myOrdersView;
        this.mContext = mContext;
    }

    @Override
    public void requestMyOrdersList(int page, String status) {
        HttpParams params = new HttpParams();
        params.put("status", status);
        params.put("page", page + "");
        OkHttpUtils.post(HttpUtil.MY_ORDERS)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, MyOrdersM.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        myOrdersView.myOrdersListSuccess((MyOrdersM) data);
                        myOrdersView.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        myOrdersView.hideLoading();
                        myOrdersView.httpStatusError();
                    }
                });
    }
}
